﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Base.Classes;
using System.Windows.Forms;
using ProgNetComponents.Design.Attributes;
using ProgNetComponentsDx.Shortcut.Design;
using System.ComponentModel;
using System.Drawing.Design;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using System.ComponentModel.Design.Serialization;

namespace ProgNetComponentsDx.Shortcut
{
    public class ShortcutSettings : BaseSettings
    {
        public ShortcutSettings(object target, ShortcutProvider provider)
            : base(target, provider)
        {
            _AllowEdit = true;
        }

        private object _ShortCutId;
        private bool _AllowEdit;
        private bool _IsCustomShortcut;
       
        #region Is Browsabele Methods
        protected bool IsShortcurBrowsable()
        {
            return ShortCutId != null || IsCustomShortcut;
        }
        #endregion

        [Editor(typeof(Design.ShortcutNameEditor), typeof(UITypeEditor))]
        [RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        [AlwaysSerialize]
        [NotifyParentProperty(true)]
        [BrowsableByPropertyValue("IsCustomShortcut", false)]
        public object ShortCutId
        {
            get { return _ShortCutId; }
            set
            {
                _ShortCutId = value;
                OnSettingsChanged();
            }
        }

        [RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        [AlwaysSerialize]
        [NotifyParentProperty(true)]
        public bool IsCustomShortcut
        {
            get { return _IsCustomShortcut; }
            set
            {
                _IsCustomShortcut = value;
                if (value)
                {
                    ShortCutId = null;
                    if (((Component)(Provider)).IsDesignMode() && CustomShortcutId.IsNull())
                    {
                        if (Owner is Control)
                            CustomShortcutId = ((Control)(Owner)).GetFullPath();
                    }
                }
                else
                {
                    CustomDefaultShortcut = Keys.None;
                    CustomShortcutId = null;
                }
            }
        }

        [NotifyParentProperty(true)]
        [BrowsableByPropertyValue("IsCustomShortcut", true)]
        [Editor(typeof(ShortcutKeyEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(KeysTypeConverter))]
        public Keys CustomDefaultShortcut
        {
            get;
            set;
        }

        [BrowsableByMethod("IsShortcurBrowsable")]
        [NotifyParentProperty(true)]
        public bool AllowEdit
        {
            get
            {
                if (IsShortcutIdSet)
                    return _AllowEdit;
                else
                    return false;
            }
            set { _AllowEdit = value; }
        }

        [BrowsableByMethod("IsShortcurBrowsable")]
        [NotifyParentProperty(true)]
        public bool AddShortCutToText
        {
            get;
            set;
        }

        [BrowsableByMethod("IsShortcurBrowsable")]
        [NotifyParentProperty(true)]
        public bool DisplayTooltip
        {
            get;
            set;
        }

        [Browsable(false)]
        public string ShortcutText
        {
            get;
            set;
        }

        [BrowsableByPropertyValue("IsCustomShortcut", true)]
        public string CustomShortcutId
        {
            get;
            set;
        }

        [Browsable(false)]
        public bool IsShortcutIdSet
        {
            get { return ShortCutId != null || (IsCustomShortcut && CustomShortcutId.IsNotNull()); }
        }

        [Browsable(false)]
        public object RealShortCutId
        {
            get
            {
                if (IsCustomShortcut)
                    return CustomShortcutId;
                return ShortCutId;
            }
        }
    }
}
